package com.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.common.ajax.AjaxRes;
import com.common.mybatis.Page;
import com.common.utils.base.Const;
import com.controller.base.BaseController;
import com.entity.Connection;
import com.entity.Product;
import com.entity.User;
import com.service.ConnectionService;
import com.service.ProductService;

@Controller
@RequestMapping("/product")
public class ProductController extends BaseController<Product>{
	
	@Autowired
	private ProductService productService;
	@Autowired
	private ConnectionService connectionService;
	
	@RequestMapping("/index")
	public String index(){
		return "pages/product/product";
	}

	@RequestMapping("/info")
	@ResponseBody
	public AjaxRes info(Product product, Page<Product> pages){
		AjaxRes res = new AjaxRes();
		pages = productService.findByPage(product, pages);
		res.setSucceed(pages, Const.DATA_SUCCEED);
		return res;
	}
	
	@RequestMapping("/all")
	@ResponseBody
	public AjaxRes all(){
		AjaxRes res = new AjaxRes();
		List<Product> pros = productService.find(null);
		res.setSucceed(pros, Const.DATA_SUCCEED);
		return res;
	}
	
	@RequestMapping("/detail")
	@ResponseBody
	public AjaxRes detail(Product product){
		AjaxRes res = new AjaxRes();
		product = productService.find(product).get(0);
		res.setSucceed(product, Const.DATA_SUCCEED);
		return res;
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public AjaxRes add(@RequestBody Map<String,Object> map){
		AjaxRes res = new AjaxRes();
		//Map 转为JSON 再反序列化为bean
		String json = JSONArray.toJSONString(map.get("obj"));
		Product product = JSON.parseArray(json, Product.class).get(0);
		User u = (User) getRequest().getSession().getAttribute("loggedIn");
		product.setPublisher(u.getUserName());
		product.setProductState(1);
		//添加默认封面
		if(product.getCover() == null || product.getCover() == ""){
			product.setCover("upload/file/empty-img.png");
		}
		productService.insert(product);
		res.setSucceedMsg(Const.SAVE_SUCCEED);
		return res;
	}
	
	@RequestMapping("/upd")
	@ResponseBody
	public AjaxRes upd(@RequestBody Map<String,Object> map){
		AjaxRes res = new AjaxRes();
		String json = JSONArray.toJSONString(map.get("obj"));
		Product product = JSON.parseArray(json, Product.class).get(0);
		//添加默认封面
		if(product.getCover() == null || product.getCover() == ""){
			product.setCover("upload/file/empty-img.png");
		}
		productService.update(product);
		res.setSucceedMsg(Const.UPDATE_SUCCEED);
		return res;
	}
	
	@RequestMapping("/pass")
	@ResponseBody
	public AjaxRes pass(Product product){
		AjaxRes res = new AjaxRes();
		product.setProductState(1);
		productService.updateState(product);
		res.setSucceedMsg(Const.UPDATE_SUCCEED);
		return res;
	}
	
	@RequestMapping("/reject")
	@ResponseBody
	public AjaxRes reject(Product product){
		AjaxRes res = new AjaxRes();
		productService.updateReject(product);
		res.setSucceedMsg(Const.UPDATE_SUCCEED);
		return res;
	}
	
	@RequestMapping("/del")
	@ResponseBody
	public AjaxRes del(Product product){
		AjaxRes res = new AjaxRes();
		productService.delete(product);
		res.setSucceed(Const.DEL_SUCCEED);
		return res;
	}
	
	@RequestMapping("/delBatch")
	@ResponseBody
	public AjaxRes delBatch(@RequestBody List<Product> products){
		AjaxRes res = new AjaxRes();
		productService.deleteBatch(products);
		res.setSucceed(Const.DEL_SUCCEED);
		return res;
	}
	
	@RequestMapping("/del/pic")
	@ResponseBody
	public AjaxRes delPic(Product product){
		AjaxRes res = new AjaxRes();
		product = productService.find(product).get(0);
		product.setCover("");
		productService.update(product);
		res.setSucceedMsg(Const.DEL_SUCCEED);
		return res;
	}
	
	// 产品对接
	@RequestMapping("/conn/index")
	public String connIndex(){
		return "pages/product/conn";
	}
	
	@RequestMapping("/conn/info")
	@ResponseBody
	public AjaxRes info(Connection conn, Page<Connection> pages){
		AjaxRes res = new AjaxRes();
		pages = connectionService.findByPage(conn, pages);
		res.setSucceed(pages, Const.DATA_SUCCEED);
		return res;
	}
	
	@RequestMapping("/conn/detail")
	@ResponseBody
	public AjaxRes detail(Connection conn){
		AjaxRes res = new AjaxRes();
		conn = connectionService.find(conn).get(0);
		res.setSucceed(conn, Const.DATA_SUCCEED);
		return res;
	}
	
	@RequestMapping("/conn/add")
	@ResponseBody
	public AjaxRes connAdd(@RequestBody Map<String,Object> map){
		AjaxRes res = new AjaxRes();
		//Map 转为JSON 再反序列化为bean
		String json = JSONArray.toJSONString(map.get("obj"));
		Connection conn = JSON.parseArray(json, Connection.class).get(0);
		User u = (User) getRequest().getSession().getAttribute("loggedIn");
		conn.setUserId(u.getId());
		connectionService.insert(conn);
		res.setSucceedMsg(Const.SAVE_SUCCEED);
		return res;
	}
	
	@RequestMapping("/conn/upd")
	@ResponseBody
	public AjaxRes connUpd(@RequestBody Map<String,Object> map){
		AjaxRes res = new AjaxRes();
		String json = JSONArray.toJSONString(map.get("obj"));
		Connection conn = JSON.parseArray(json, Connection.class).get(0);
		connectionService.update(conn);
		res.setSucceedMsg(Const.UPDATE_SUCCEED);
		return res;
	}
	
	@RequestMapping("/conn/del")
	@ResponseBody
	public AjaxRes del(Connection conn){
		AjaxRes res = new AjaxRes();
		connectionService.delete(conn);
		res.setSucceed(Const.DEL_SUCCEED);
		return res;
	}
	
	@RequestMapping("/conn/delBatch")
	@ResponseBody
	public AjaxRes connDelBatch(@RequestBody List<Connection> conns){
		AjaxRes res = new AjaxRes();
		connectionService.deleteBatch(conns);
		res.setSucceed(Const.DEL_SUCCEED);
		return res;
	}
	
}
